package com.tql.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author: tyk
 * @Date: 2023/5/21 19:21
 * @Description: 跨域配置
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {

        // 跨域配置对象
        CorsConfiguration cfg = new CorsConfiguration();
        cfg.addAllowedOrigin("*");
        cfg.setAllowCredentials(true);
        cfg.addAllowedMethod("*");
        cfg.addAllowedHeader("*");

        // 配置源对象
        UrlBasedCorsConfigurationSource ccs = new UrlBasedCorsConfigurationSource();
        ccs.registerCorsConfiguration("/**", cfg);  // configuration作用的路径为"/**"

        return new CorsWebFilter(ccs);
    }
}
